package com.kingwood.algorithm.test;

import java.util.Arrays;
import java.util.Scanner;

// https://fcqian.blog.csdn.net/article/details/135118816
// 堆内存申请
public class DuiMemory {

    public static int getResult(int[] memory, int memorySize, int rqSize) {
        if (memorySize < rqSize) {
            return -1;
        }

        int preStart = -1;
        int preSize = Integer.MAX_VALUE;
        int start = -1;
        int curSize = 0;
        for (int i=0; i<100; i++) {
            if (memory[i] == 0) {
                if (start == -1) {
                    start = i;
                }
                curSize++;
            } else {
                if (rqSize > curSize) {
                    // 不满足
                    start = -1;
                    curSize = 0;
                } else {
                    if (preSize > curSize) {
                        preSize = curSize;
                        preStart = start;
                    }
                    start = -1;
                    curSize = 0;
                }
            }
        }

        if (preSize > curSize) {
            preSize = curSize;
            preStart = start;
        }

        return preStart;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] memory = new int[100];
        int memorySize = 100;
        int rqSize = Integer.parseInt(scanner.nextLine());

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            int[] tmp = Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray();
            int start = tmp[0];
            for (int i=0; i<tmp[1]; i++) {
                memory[start + i] = 1;
            }
            memorySize -= tmp[1];
        }

        System.out.println(Arrays.toString(memory));
        System.out.println(getResult(memory, memorySize, rqSize));
    }
}
